﻿using Newtonsoft.Json.Linq;
using ProjScan.Tools;
using System;
using System.Collections.Generic;

namespace ProjScan.Config
{
    //
    // 摘要:
    //     模块配置信息的基类 - Class2
    [Serializable]
    internal abstract class BaseConfig
    {
        public Dictionary<string, RuleConfig> dicRuleConfig = new Dictionary<string, RuleConfig>();

        protected JObject _rootConfig = null;

        //
        // 摘要:
        //     拥有单独配置 选项
        public bool hasSeparateTargetDirSettings;

        //
        // 摘要:
        //     拥有白名单
        public bool hasWhiteList;

        //
        // 摘要:
        //     是否拥有不符合规范文件的信息
        public bool hasfailedData;

        //
        // 摘要:
        //     是否拥有修改规则
        public bool hasFixRule;

        public abstract void Init();

        public RuleConfig Get(string key)
        {
            dicRuleConfig.TryGetValue(key, out RuleConfig value);
            return value;
        }

        protected virtual void ResolveRule()
        {
            JObject jObject = (JObject)_rootConfig["Rule_List"];
            foreach (KeyValuePair<string, JToken> item3 in jObject)
            {
                string key = item3.Key;
                JObject jObject2 = (JObject)item3.Value;
                bool checkEnabled = (jObject2["checkEnabled"].Value<bool>() ? ((byte)1) : ((byte)0)) != 0;
                bool? checkEnabled2 = Singleton<QAOnlineSettings>.Instance.GetCheckEnabled(key);
                if (checkEnabled2.HasValue)
                {
                    checkEnabled = checkEnabled2.Value;
                }

                bool hasSrcPath = (jObject2["hasSrcPath"].Value<bool>() ? ((byte)1) : ((byte)0)) != 0;
                bool isSeparateDirSettingsEnabled = false;
                if (jObject2["SeparateDirSettingsEnabled"] != null)
                {
                    isSeparateDirSettingsEnabled = ((jObject2["SeparateDirSettingsEnabled"].Value<bool>() ? ((byte)1) : ((byte)0)) != 0);
                }

                bool fixEnabled = (jObject2["fixEnabled"].Value<bool>() ? ((byte)1) : ((byte)0)) != 0;
                RuleConfig value;
                if (hasSeparateTargetDirSettings)
                {
                    List<ScanAssetsConfig> list = new List<ScanAssetsConfig>();
                    JArray jArray = (JArray)jObject2["target_config"];
                    foreach (JObject item4 in jArray)
                    {
                        ScanAssetsConfig item = JObjectToScanAssetsConfig(item4);
                        list.Add(item);
                    }

                    List<ScanAssetsConfig> list2 = new List<ScanAssetsConfig>();
                    JArray jArray2 = (JArray)jObject2["ignore_config"];
                    foreach (JObject item5 in jArray2)
                    {
                        ScanAssetsConfig item2 = JObjectToScanAssetsConfig(item5);
                        list2.Add(item2);
                    }

                    value = default(RuleConfig);
                    value.ruleKey = key;
                    value.checkEnabled = checkEnabled;
                    value.fixEnabled = fixEnabled;
                    value.isSeparateDirSettingsEnabled = isSeparateDirSettingsEnabled;
                    value.hasSrcPath = hasSrcPath;
                    value.targetConfig = list.ToArray();
                    value.ignoreConfig = list2.ToArray();
                    dicRuleConfig.Add(key, value);
                }
                else
                {
                    value = default(RuleConfig);
                    value.ruleKey = key;
                    value.checkEnabled = checkEnabled;
                    value.fixEnabled = fixEnabled;
                    value.hasSrcPath = hasSrcPath;
                    dicRuleConfig.Add(key, value);
                }
            }
        }

        protected virtual JObject SaveJObjectRule()
        {
            JObject jObject = new JObject();
            JObject jObject2 = new JObject();
            JObject jObject3 = (JObject)_rootConfig["Rule_List"];
            jObject.Add("Rule_List", jObject2);
            foreach (KeyValuePair<string, RuleConfig> item in dicRuleConfig)
            {
                RuleConfig value = item.Value;
                JObject jObject4 = (JObject)jObject3[item.Key];
                if (jObject4["checkEnabled"] == null)
                {
                    jObject4.Add("checkEnabled", (JToken)value.checkEnabled);
                }
                else
                {
                    jObject4["checkEnabled"] = (JToken)value.checkEnabled;
                }

                if (jObject4["fixEnabled"] == null)
                {
                    jObject4.Add("fixEnabled", (JToken)value.fixEnabled);
                }
                else
                {
                    jObject4["fixEnabled"] = (JToken)value.fixEnabled;
                }

                if (hasSeparateTargetDirSettings)
                {
                    JArray value2 = ScanAssetsConfigsToJArray(value.targetConfig);
                    JArray value3 = ScanAssetsConfigsToJArray(value.ignoreConfig);
                    if (jObject4["target_config"] == null)
                    {
                        jObject4.Add("target_config", value2);
                    }
                    else
                    {
                        jObject4["target_config"] = value2;
                    }

                    if (jObject4["ignore_config"] == null)
                    {
                        jObject4.Add("ignore_config", value3);
                    }
                    else
                    {
                        jObject4["ignore_config"] = value3;
                    }

                    if (jObject4["SeparateDirSettingsEnabled"] == null)
                    {
                        jObject4.Add("SeparateDirSettingsEnabled", (JToken)value.isSeparateDirSettingsEnabled);
                    }
                    else
                    {
                        jObject4["SeparateDirSettingsEnabled"] = (JToken)value.isSeparateDirSettingsEnabled;
                    }
                }

                jObject2.Add(value.ruleKey, jObject4);
            }

            return jObject;
        }

        protected JArray ScanAssetsConfigsToJArray(ScanAssetsConfig[] scanAssetsConfigs)
        {
            JArray jArray = new JArray();
            for (int i = 0; i < scanAssetsConfigs.Length; i++)
            {
                ScanAssetsConfig scanAssetsConfig = scanAssetsConfigs[i];
                JObject jObject = new JObject
                {
                    {
                        "matching_str",
                        (JToken)scanAssetsConfig.assetPath
                    },
                    {
                        "matching_type",
                        (JToken)(int)scanAssetsConfig.matchingType
                    }
                };
                if (scanAssetsConfig.thresholds != null && scanAssetsConfig.thresholds.Length != 0)
                {
                    JArray jArray2 = new JArray();
                    for (int j = 0; j < scanAssetsConfig.thresholds.Length; j++)
                    {
                        jArray2.Add(scanAssetsConfig.thresholds[j]);
                    }

                    jObject.Add("thresholds", jArray2);
                    jObject.Add("priority", (JToken)(int)scanAssetsConfig.priority);
                }

                jArray.Add(jObject);
            }

            return jArray;
        }

        protected ScanAssetsConfig JObjectToScanAssetsConfig(JObject data)
        {
            ScanAssetsConfig result = default(ScanAssetsConfig);
            result.assetPath = data["matching_str"]!.ToString();
            result.matchingType = ((data["matching_type"].Value<int>() != 0) ? MatchingType.regex : MatchingType.directory);
            if (data.TryGetValue("priority", out JToken _))
            {
                result.priority = (ThresholdPriority)data["priority"].Value<int>();
                JArray jArray = (JArray)data["thresholds"];
                if (jArray.Count > 0)
                {
                    string[] array = new string[jArray.Count];
                    for (int i = 0; i < jArray.Count; i++)
                    {
                        array[i] = jArray[i].Value<string>();
                    }

                    result.thresholds = array;
                }
            }

            return result;
        }

        //
        // 摘要:
        //     添加扫描路径
        public void AddScanAssets(string ruleKey, ScanAssetsConfig scanAssets)
        {
        }

        //
        // 摘要:
        //     移除扫描路径
        //
        // 参数:
        //   ruleKey:
        //
        //   scanAssets:
        public void RemoveScanAssets(string ruleKey, ScanAssetsConfig scanAssets)
        {
        }

        public abstract void LoadConfig(JObject data);

        public abstract void SaveConfig(JObject data);
    }
}